package com.ztf.shopping.web.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ztf.shopping.service.UserService;
import com.ztf.shopping.service.imp.UserServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/findNameServlet")
public class FindNameServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String name = request.getParameter("name");
        ObjectMapper om = new ObjectMapper();
        Map<String,String> map = new HashMap<String, String>();
        if("".equals(name)){
            //注册失败
            map.put("message","不可用");
            om.writeValue(response.getOutputStream(),map);
            return;
        }

        UserService userService = new UserServiceImp();
        String findName = userService.registeredFindName(name);

        if (findName==null){
            //注册成功
            map.put("message","可使用");
            om.writeValue(response.getOutputStream(),map);
        }else {
            //注册失败
            map.put("message","不可用");
            om.writeValue(response.getOutputStream(),map);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
